#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back
#define vec vector<int>

int read(){int x = 0,sgn = 1;char ch = getchar();for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);return x*sgn;}

const int N = 2e5+10;

priority_queue <int> q[N];

int id[N],m[N],n,h[N],cnt,top,stk[N],f[N];

void merge(int &x,int &y){
	if(q[x].size() < q[y].size()) swap(x,y);
	while(!q[y].empty()){
		int a = q[x].top(),b = q[y].top();
		stk[++top] = max(a,b); q[x].pop(); q[y].pop();
	}
	while(top) q[x].push(stk[top--]);
}

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in; for(int i = 1;i <= n;i++) m[i] = in;
	for(int i = 2;i <= n;i++) f[i] = in;
	for(int i = 1;i <= n;i++) id[i] = i;
	for(int i = n;i >= 2;i--) q[id[i]].push(m[i]),merge(id[f[i]],id[i]);
	q[id[1]].push(m[1]); ll ans = 0;
	while(!q[id[1]].empty()) ans += q[id[1]].top(),q[id[1]].pop();
	printf("%lld\n",ans);
	return 0;
}
